<template>
  <div class="fanpage">
    <!-- 头部 -->
    <Navbar title="粉丝" placeholder bgColor="#161622">
      <template #left>
        <div class="back-btn" @click="handleBack">
          <Icon name="zuojiantou" color="#fff" size="18"></Icon>
        </div>
      </template>
    </Navbar>
    <!-- 粉丝列表 -->
    <div class="fans-list">
      <div v-for="fan in fans" :key="fan.id" class="fan-item">
        <!-- 头像 -->
        <div class="avatar-container">
          <img :src="fan.avatar" alt="头像" class="avatar" />
        </div>

        <!-- 内容 -->
        <div class="content">
          <div class="nickname">{{ fan.userNickname }}</div>
          <div class="status">关注了你</div>
          <div class="time">{{ fan.time }}</div>
        </div>

        <!-- 操作按钮 -->
        <button class="follow-btn" @click="handleFollow(fan)" :class="{ 'active': fan.bothStatus != 1 }">
          <!-- 互相关注 -->
          {{ fan.bothStatus == 1 ? '互相关注' : '关注' }}
        </button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import Navbar from '@/components/common/Navbar.vue'
import Icon from '@/components/common/Icon.vue'
import { formatTimeAgo } from '@/utils/time'
import { Toast } from '@/utils/showtoast'
import { useUserStore } from '@/stores/userinfo'
const userStore = useUserStore()
const { userId } = userStore.userInfo
import { useRouter } from 'vue-router'
const router = useRouter()
import { getFanListAPI, followFanAPI } from '@/api/message'



// 模拟数据
const fans = ref([])

// 互相关注事件
const handleFollow = async ({ userId: fanId }) => {
  // 找到当前点击的粉丝
  const fan = fans.value.find((item) => item.userId === fanId)

  try {

    const res = await followFanAPI(userId, fanId)
    if (res.code === 200) {
      fan.bothStatus = fan.bothStatus == 1 ? 0 : 1
      Toast(res.data, {
        position: 'bottom',
      })
    }
  } catch (error) {
    Toast('互相关注失败', {
      position: 'bottom',
    })
  }

}


// 返回
const handleBack = () => router.back()

// 获取粉丝列表
const getFansList = async () => {
  try {
    const res = await getFanListAPI(userId)
    // console.log(res)
    if (res.code === 200) {
      res.data.map((item) => {
        item.avatar = 'http://43.138.15.137:3000' + item.userAvatar,
          item.time = formatTimeAgo(item.createdAt)
      })
      fans.value = res.data
    }

  } catch (error) {
    console.error('获取粉丝列表失败:', error)
  }
}
onMounted(() => getFansList())


</script>

<style lang="scss" scoped>
.fanpage {
  width: 100%;
  min-height: 100vh;
  background-color: #1d1f2a;
  color: #fff;
}

.fans-list {
  padding: 16px;
}

.fan-item {
  display: flex;
  align-items: center;
  padding: 10px 0;
  border-bottom: 1px solid #2a2a2a;

  &:last-child {
    padding-bottom: 40px;
    border-bottom: none;
  }

  .avatar-container {
    width: 48px;
    height: 48px;
    border-radius: 50%;
    overflow: hidden;
    margin-right: 12px;
  }

  .content {
    flex: 1;
    min-width: 0;

    .nickname {
      font-size: 16px;
      font-weight: 500;
      margin: 0;
    }

    .status {
      font-size: 14px;
      color: #ccc;
      margin: 4px 0;
    }

    .time {
      font-size: 12px;
      color: #999;
    }
  }

  .follow-btn {
    width: 70px;
    height: 30px;
    background-color: #444;
    color: #fff;
    border-radius: 4px;
    font-size: 14px;
    cursor: pointer;
    transition: background-color 0.2s ease;

    &:hover {
      background-color: #555;
    }

    &.active {
      background-color: #f9355f;
      color: $white-color;
    }
  }
}
</style>